﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace winproject.Models
{
    public class CategoryDetails
    {
        public int nItemId { get; set; }
        public string sItemName { get; set; }
        public string sItemSlug { get; set; }
        [AllowHtml]
        public string sItemDescription { get; set; }
        public string sItemImagePath { get; set; }
        public byte? nItemStatus { get; set; }
    }

    public class CategoryModel
    {
        #region Add New Item Detail
        public ReturnResult AddNewCategory(CategoryDetails newItem, bool _isEditReq = false)
        {
            ReturnResult oResult = null;


            try
            {
                var oWinContext = new WinProductsDataContext(Convert.ToString(HttpContext.Current.Application["constr"]));
                var oItemupdate = oWinContext.Categories.Where(c => c.ID == newItem.nItemId).FirstOrDefault();
                Category oItem = new Category();
                if (oWinContext.DatabaseExists())
                {
                    oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.RecordNotInserted, msgJson = "OOps Something went wrong !!" };
                    if (oItemupdate != null)
                    {
                        //simply update virus info
                        oItemupdate.Cat_Name = newItem.sItemName;
                        oItemupdate.Cat_Slug = newItem.sItemSlug;
                        oItemupdate.Cat_Description = newItem.sItemDescription;
                        if (!_isEditReq)
                            oItemupdate.Status = 1;

                        oWinContext.SubmitChanges();
                        oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Success, msgJson = string.Format("Record {0} Successfully !!", "Updated") };
                    }
                    else
                    {

                        oItem.Cat_Name = newItem.sItemName;
                        oItem.Cat_Slug = newItem.sItemSlug;
                        oItem.Cat_Description = newItem.sItemDescription;
                        oItem.Status = 1;
                        oWinContext.Categories.InsertOnSubmit(oItem);
                        oWinContext.SubmitChanges();
                        oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Success, msgJson = string.Format("Record {0} Successfully !!", "Added") };
                    }
                }
            }
            catch (Exception ex)
            {
                oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Exception, msgJson = string.Format("Exception: {0}", ex.ToString()) };
            }
            return oResult;

        }
        #endregion

        #region Find Category Details - Filters : Slug, ID, or Get ALL Item details
        public ReturnResult getCategoryDetails(out List<CategoryDetails> oIDetailList, CategoryDetails oSearchItem = null)
        {
            ReturnResult oResult = null;
            oIDetailList = null;
            try
            {

                var oWinContext = new WinProductsDataContext(Convert.ToString(HttpContext.Current.Application["constr"]));
                if (oWinContext.DatabaseExists())
                {
                    oIDetailList = new List<CategoryDetails>();
                    if (oSearchItem != null && (!string.IsNullOrEmpty(oSearchItem.sItemName) || !string.IsNullOrEmpty(oSearchItem.sItemSlug)))
                    {
                        //search request
                        oIDetailList = oWinContext.Categories.Where(c => c.Cat_Slug == oSearchItem.sItemSlug)
                                                                .Select(c => new CategoryDetails()
                                                                {
                                                                    nItemId = c.ID,
                                                                    sItemName = c.Cat_Name,
                                                                    sItemSlug = c.Cat_Slug,
                                                                    sItemDescription = c.Cat_Description,
                                                                    nItemStatus = c.Status
                                                                }).ToList<CategoryDetails>();
                    }
                    else if (oSearchItem != null && (oSearchItem.nItemId) > 0)
                    {
                        //search request
                        oIDetailList = oWinContext.Categories.Where(c => c.ID == oSearchItem.nItemId)
                                                                .Select(c => new CategoryDetails()
                                                                {
                                                                    nItemId = c.ID,
                                                                    sItemName = c.Cat_Name,
                                                                    sItemSlug = c.Cat_Slug,
                                                                    sItemDescription = c.Cat_Description,
                                                                    nItemStatus = c.Status
                                                                }).ToList<CategoryDetails>();
                    }
                    else
                    {
                        oIDetailList = oWinContext.Categories.Select(c => new CategoryDetails()
                        {
                            nItemId = c.ID,
                            sItemName = c.Cat_Name,
                            sItemSlug = c.Cat_Slug,
                            sItemDescription = c.Cat_Description,
                            nItemStatus = c.Status
                        }).ToList<CategoryDetails>();
                    }

                    //var lsCategories = qCatList.ToList<Category>();
                    string resultJson = JsonConvert.SerializeObject(oIDetailList);
                    if (oIDetailList != null && oIDetailList.Count > 0)
                    {
                        if (!string.IsNullOrEmpty(resultJson))
                        {
                            oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Success, msgJson = resultJson };
                        }
                    }
                    else
                    {
                        oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.NoRecordsFound, msgJson = resultJson };
                    }
                }
            }
            catch (Exception ex)
            {
                oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Exception, msgJson = string.Format("Exception: {0}", ex.ToString()) };
            }
            return oResult;

        }
        #endregion

        #region Delete Category Detail
        public ReturnResult deleteCategory(CategoryDetails oItem)
        {
            ReturnResult oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.BadRequest, msgJson = "[]" };
            try
            {
                if (oItem.nItemId > 0)
                {
                    var oWinContext = new WinProductsDataContext(Convert.ToString(HttpContext.Current.Application["constr"]));
                    if (oWinContext.DatabaseExists())
                    {
                        Category oItemInfo = oWinContext.Categories.Where(x => x.ID == oItem.nItemId).FirstOrDefault();
                        if (oItem.nItemId > 0)
                        {
                            //means record exist
                            oWinContext.Categories.DeleteOnSubmit(oItemInfo);
                            oWinContext.SubmitChanges(ConflictMode.FailOnFirstConflict);
                            if (oItemInfo != null && oItemInfo.ID > 0)
                            {
                                oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Success, msgJson = "Record removed successfully" };
                            }
                            else
                            {
                                oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.RecordNotDeleted, msgJson = "No record deleted" };
                            }
                        }
                        else
                        {
                            oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.NoRecordsFound, msgJson = "No record found" };
                        }
                    }
                }
            }
            catch (ChangeConflictException e)
            {
                oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Exception, msgJson = string.Format("Exception: {0}", e.ToString()) };
            }
            catch (Exception ex)
            {
                oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Exception, msgJson = string.Format("Exception: {0}", ex.ToString()) };
            }
            return oResult;

        }
        #endregion

    }
}